package day02;

import exception.DataAccessException;
import util.JdbcUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/showModify")
public class ShowModifyEmpServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("ShowModifyEmpServlet.service()");

        response.setContentType("text/html;charset=utf-8");

        PrintWriter out = response.getWriter();

        Integer id = Integer.parseInt(request.getParameter("id"));

        System.out.println("id = " + id);

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = JdbcUtil.getConnection();

            con.setAutoCommit(false);


            String sql = new StringBuffer()
                    .append("select * ")
                    .append("from t_emp ")
                    .append("where id=? ")
                    .toString();

            ps = con.prepareStatement(sql);

            ps.setInt(1, id);

            rs = ps.executeQuery();

            if (rs.next()) {

                String name = rs.getString("name");
                Double salary = rs.getDouble("salary");
                String sex = rs.getString("sex");
                String edu = rs.getString("edu");
                String hobbies = rs.getString("hobbies");
                String description = rs.getString("description");

                out.print("<!DOCTYPE html>");
                out.print("<html lang=\"en\">");
                out.print("    <head>");
                out.print("        <meta charset=\"UTF-8\">");
                out.print("        <title>Title</title>");
                out.print("    </head>");
                out.print("    <body>");
                out.print("        <form action=\"/web/modify\" method=\"post\">");
                out.print("            <input type='hidden' name='id' value='" + id + "'/>");
                out.print("            姓名：<input type=\"text\" name=\"name\" value='" + name + "'/><br/>");
                out.print("            工资：<input type=\"password\" name=\"salary\" value='" + salary + "'/><br/>");
                out.print("            性别：<input type=\"radio\" name=\"sex\" value=\"1\" " + ("1".equals(sex) ? "checked='checked'" : "") + " />男");
                out.print("                 <input type=\"radio\" name=\"sex\" value=\"0\" " + ("0".equals(sex) ? "checked='checked'" : "") + " />女<br/>");
                out.print("            学历：<select name=\"edu\">");
                out.print("                    <option>---请选择---</option>");
                out.print("                    <option value=\"1\" " + ("1".equals(edu) ? "selected='selected'" : "") + " >高中</option>");
                out.print("                    <option value=\"2\" " + ("2".equals(edu) ? "selected='selected'" : "") + " >专科</option>");
                out.print("                    <option value=\"3\" " + ("3".equals(edu) ? "selected='selected'" : "") + " >本科</option>");
                out.print("                 </select><br/>");
                out.print("            爱好：<input type=\"checkbox\" name=\"hobbies\" value=\"1\" " + getChecked("1", hobbies) + " />吃");
                out.print("                 <input type=\"checkbox\" name=\"hobbies\" value=\"2\" " + getChecked("2", hobbies) + " />玩");
                out.print("                 <input type=\"checkbox\" name=\"hobbies\" value=\"3\" " + getChecked("3", hobbies) + " />睡<br/>");
                out.print("            简介：<textarea rows=\"5\" cols=\"20\" name=\"description\">" + description + "</textarea><br/>");
                out.print("            <input type=\"submit\" value=\"修改\"/>");
                out.print("        </form>");
                out.print("    </body>");
                out.print("</html>");


            }


            con.commit();

        } catch (Exception e) {
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
            throw new DataAccessException("数据访问失败", e);
        } finally {
            JdbcUtil.close(con, ps, rs);
        }


    }


    private String getChecked(String value, String hobbies) {
        if (hobbies != null) {
            String[] s = hobbies.split(",");
            for (String hobby : s) {
                if (hobby.equals(value)) {
                    return "checked='checked'";
                }
            }
        }
        return "";
    }
}
